1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
#include <errno.h>

/* Simple error handling functions */

#define handle_error_en(en, msg) \
do { errno = en; perror(msg); exit(EXIT_FAILURE); } while (0)

static void *
sig_thread(void *arg)
{
printf("yyyyy, thread id is: %ld\n", pthread_self());
sigset_t aset;
int s, sig;
sigemptyset(&aset);
sigaddset(&aset, SIGQUIT);
sigaddset(&aset, SIGUSR1);
//s = pthread_sigmask(SIG_BLOCK, &aset, NULL);
sigset_t *set = (sigset_t *)arg;

for (;;) {
s = sigwait(set, &sig);
if (s != 0)
handle_error_en(s, "sigwait");
printf("Signal handling thread got signal %d\n", sig);
}
}

static void handler(int arg)
{
printf("xxxxx, thread id is: %ld\n", pthread_self());
}

int main(int argc, char *argv[])
{
pthread_t thread;
sigset_t set;
int s;

/* Block SIGINT; other threads created by main() will inherit
* a copy of the signal mask. */

signal(SIGQUIT, handler);
// if (s != 0)
// handle_error_en(s, "pthread_sigmask");

s = pthread_create(&thread, NULL, &sig_thread, (void *)&set);
sigemptyset(&set);
sigaddset(&set, SIGQUIT);
sigaddset(&set, SIGUSR1);
//s = pthread_sigmask(SIG_BLOCK, &set, NULL);
if (s != 0)
handle_error_en(s, "pthread_create");
printf("sub thread with id: %ld\n", thread);
/* Main thread carries on to create other threads and/or do
* other work */

pause(); /* Dummy pause so we can test program */
}

 评论